package com.my.ollama.controller;

import com.my.common.result.R;
import com.my.ollama.model.OllamaRequest;
import com.my.ollama.service.OllamaService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author mars
 * @date 2025-01-27 20:38
 */
@RestController
@RequestMapping("/ollama")
@RequiredArgsConstructor
public class OllamaController {

    private final OllamaService ollamaService;

    @PostMapping("/chat")
    public R<String> chat(@RequestBody OllamaRequest request) {
        return R.success(ollamaService.chat(request));
    }

    @PostMapping(value = "/stream/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> streamChat(@RequestBody OllamaRequest request) {
       return Flux.from(ollamaService.streamChat(request).mapNotNull(e -> e.getResult().getOutput().getText()));
    }
}
